#ifndef _LIST_H_
#define _LIST_H_

#include <list_t.h>

KINLINE void __list_add 
    (
    list_t * node, 
    list_t * prev, 
    list_t * next
    )
    {
    next->prev = node;
    node->next = next;
    prev->next = node;
    node->prev = prev;

    return;
    }   

KINLINE void __list_del 
    (
    list_t * prev, 
    list_t * next
    )
    {
    next->prev = prev;
    prev->next = next;

    return;
    }

KINLINE void list_init 
    (
    list_t * list
    )
    {
    list->prev = list;
    list->next = list;

    return;    
    }


/*********************************************************************************
 * 
 * list_add - 给指定的链表添加一个节点
 * 
 * DESCRIPTION
 * 
 */

KINLINE void list_add 
    (
    list_t * node, 
    list_t * head
    )
    {
    __list_add (node, head, head->next);

    return;
    }

/*********************************************************************************
 * 
 * list_add_tail - 给指定的链表尾端添加一个节点
 * 
 * DESCRIPTION
 * 
 */

KINLINE void list_add_tail 
    (
    list_t * node, 
    list_t * head
    )
    {
    __list_add (node, head->prev, head);

    return;
    }

KINLINE void list_del_entry 
    (
    list_t * entry
    )
    {
    __list_del (entry->prev, entry->next);

    return;
    }

/*********************************************************************************
 * 
 * list_del - 把指定的链表删除一个节点
 * 
 * DESCRIPTION
 * 
 */

KINLINE void list_del
    (
    list_t * entry
    )
    {
    __list_del (entry->prev, entry->next);
    list_init (entry);

    return;
    }

/*********************************************************************************
 * 
 * list_move - 把指定的链表移动一个节点
 * 
 * DESCRIPTION
 * 
 */

KINLINE void list_move 
    (
    list_t * list, 
    list_t * head
    )
    {
    list_del (list);
    list_add (list, head);

    return;
    }

/*********************************************************************************
 * 
 * list_move_tail - 把指定的链表上的一个节点移动到链表尾端
 * 
 * DESCRIPTION
 * 
 */

KINLINE void list_move_tail
    (
    list_t * list, 
    list_t * head
    )
    {
    list_del (list);
    list_add_tail (list, head);

    return;
    }    

/*********************************************************************************
 * 
 * list_is_empty - 判断链表是否为空
 * 
 * DESCRIPTION
 * 
 */

KINLINE bool_t list_is_empty 
    (
    const list_t * head
    )
    {
    if (head->next == head)
        {
        return TRUE;
        }

    return FALSE;
    }

/*********************************************************************************
 * 
 * list_is_empty_careful - 安全的判断链表是否为空
 * 
 * DESCRIPTION
 * 
 */

KINLINE bool_t list_is_empty_careful 
    (
    const list_t * head
    )
    {
    list_t * next = head->next;
    
    if ((next == head) && (next == head->prev))
        {
        return TRUE;
        }

    return FALSE;
    }

/*********************************************************************************
 * 
 * list_entry - 访问一个节点
 * 
 * DESCRIPTION
 * 
 */


#define list_entry(ptr, type, member) \
        ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))


/*********************************************************************************
 * 
 * list_for_each - 遍历一个链表
 * 
 * DESCRIPTION
 * 
 */

#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = (pos)->next)

#endif